package ui.swing.list;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class List_Double extends JFrame implements MouseListener, ActionListener{

	private static final long serialVersionUID = -4959864400219590408L;

	private JList v_lstLeft;
	private JList v_lstRight;
	private DefaultListModel v_lstmLeft;
	private DefaultListModel v_lstmRight;
	private JButton v_btnLeftToRight;

	private JButton v_btnRightToLeft;
	private JButton v_btnAllLeftToRight;
	private JButton v_btnAllRightToLeft;
	
	private String[] v_lstContnets;
	
	public List_Double() {
		super("列表选择框");
		setBounds(200, 200, 500, 400);
		
		JPanel cp = new JPanel(new BorderLayout());
		
		JPanel main = new JPanel(new GridBagLayout());
		Insets margin = new Insets(5, 5, 5, 5);
		main.setBorder(new CompoundBorder(new EmptyBorder(margin), new TitledBorder("请选择旅游目的地：")));
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		c.anchor = GridBagConstraints.CENTER;
		main.add(getV_lstLeft(), c);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		buttonPanel.setPreferredSize(new Dimension(50,200));
		buttonPanel.add(Box.createVerticalGlue());
		buttonPanel.add(getV_btnLeftToRight());
		buttonPanel.add(Box.createVerticalGlue());
		buttonPanel.add(getV_btnRightToLeft());
		buttonPanel.add(Box.createVerticalGlue());
		buttonPanel.add(getV_btnAllLeftToRight());
		buttonPanel.add(Box.createVerticalGlue());
		buttonPanel.add(getV_btnAllRightToLeft());
		buttonPanel.add(Box.createVerticalGlue());
		
		getV_btnLeftToRight().addActionListener(this);
		getV_btnRightToLeft().addActionListener(this);
		getV_btnAllLeftToRight().addActionListener(this);
		getV_btnAllRightToLeft().addActionListener(this);
		
		c.gridx = 2;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0.0;
		c.weighty = 0.0;
		c.anchor = GridBagConstraints.CENTER;
		main.add(buttonPanel, c);
		
		c.gridx = 3;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		c.anchor = GridBagConstraints.CENTER;
		main.add(getV_lstRight(), c);
		cp.add(main, BorderLayout.CENTER);
		
		getV_lstLeft().addMouseListener(this);
		getV_lstRight().addMouseListener(this);
		
		setContentPane(cp);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	private JList getV_lstLeft() {
		if(v_lstLeft == null) {
			v_lstLeft = new JList(getV_lstmLeft());
			v_lstLeft.setBorder(new EmptyBorder(5, 5, 5, 5));
			v_lstLeft.setPreferredSize(new Dimension(150, 200));
		}
		return v_lstLeft;
	}
	private JList getV_lstRight() {
		if(v_lstRight == null) {
			v_lstRight = new JList(getV_lstmRight());
			v_lstRight.setBorder(new EmptyBorder(5, 5, 5, 5));
			v_lstRight.setPreferredSize(new Dimension(150, 200));
		}
		return v_lstRight;
	}
	private DefaultListModel getV_lstmLeft() {
		if(v_lstmLeft == null) {
			v_lstmLeft = new DefaultListModel();
			for(String value: getV_lstContnets()) {
				v_lstmLeft.addElement(value);
			}
		}
		return v_lstmLeft;
	}
	private DefaultListModel getV_lstmRight() {
		if(v_lstmRight == null) {
			v_lstmRight = new DefaultListModel();
		}
		return v_lstmRight;
	}
	private String[] getV_lstContnets() {
		if(v_lstContnets == null) {
			v_lstContnets = new String[]{"中国","美国","俄罗斯","英国","法国","意大利","澳洲","韩国"};
		}
		return v_lstContnets;
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount() == 2) {
			JList sourceList;
			JList destList;
			if(e.getSource() == getV_lstLeft()) {
				sourceList = getV_lstLeft();
				destList = getV_lstRight();
			} else if(e.getSource() == getV_lstRight()) {
				sourceList = getV_lstRight();
				destList = getV_lstLeft();
			} else {
				return;
			}
			DefaultListModel modelSource = (DefaultListModel)sourceList.getModel();
			DefaultListModel modelDest = (DefaultListModel)destList.getModel();

			int selIndex = sourceList.locationToIndex(e.getPoint());
			modelDest.addElement(modelSource.remove(selIndex));
		}
	}

	public void actionPerformed(ActionEvent e) {
		JList sourceList;
		JList destList;
		Object[] selObjs;
		
		if(e.getSource() == getV_btnLeftToRight()) {
			sourceList = getV_lstLeft();
			destList = getV_lstRight();
			selObjs = sourceList.getSelectedValues();
		} else if (e.getSource() == getV_btnRightToLeft()) {
			sourceList = getV_lstRight();
			destList = getV_lstLeft();
			selObjs = sourceList.getSelectedValues();
		} else if (e.getSource() == getV_btnAllLeftToRight()) {
			sourceList = getV_lstLeft();
			destList = getV_lstRight();
		} else if (e.getSource() == getV_btnAllRightToLeft()) {
			sourceList = getV_lstRight();
			destList = getV_lstLeft();
		} else {
			return;
		}
		selObjs = sourceList.getSelectedValues();
		
		
		DefaultListModel modelSource = (DefaultListModel)sourceList.getModel();
		DefaultListModel modelDest = (DefaultListModel)destList.getModel();

		if(e.getSource() == getV_btnLeftToRight() || e.getSource() == getV_btnRightToLeft()) {
			if(selObjs.length == 0)
				return;
			for(Object selObj : selObjs) {
				modelSource.removeElement(selObj);
				modelDest.addElement(selObj);
			}
		}
		else {
			while(modelSource.getSize()>0) {
				modelDest.addElement(modelSource.remove(0));
			}
		}
	}

	private JButton getV_btnLeftToRight() {
		if(v_btnLeftToRight == null) {
			v_btnLeftToRight = new JButton(" > ");
			v_btnLeftToRight.setPreferredSize(new Dimension(50,20));
		}
		return v_btnLeftToRight;
	}

	private JButton getV_btnRightToLeft() {
		if(v_btnRightToLeft == null) {
			v_btnRightToLeft = new JButton(" < ");
			v_btnRightToLeft.setPreferredSize(new Dimension(50,20));
		}
		return v_btnRightToLeft;
	}

	private JButton getV_btnAllLeftToRight() {
		if(v_btnAllLeftToRight == null) {
			v_btnAllLeftToRight = new JButton(">>");
			v_btnAllLeftToRight.setPreferredSize(new Dimension(50,20));
		}
		return v_btnAllLeftToRight;
	}

	private JButton getV_btnAllRightToLeft() {
		if(v_btnAllRightToLeft == null) {
			v_btnAllRightToLeft = new JButton("<<");
			v_btnAllRightToLeft.setPreferredSize(new Dimension(50,20));
		}
		return v_btnAllRightToLeft;
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
	
	public static void main(String[] args) {
		new List_Double();
	}
	
}
